package com.gxc.healthfood.controller;

import com.gxc.healthfood.common.ResponseResult;
import com.gxc.healthfood.entity.FoodCategories;
import com.gxc.healthfood.service.FoodCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 食物分类控制器
 */
@RestController
@RequestMapping("/food/category")
public class FoodCategoryController {

    @Autowired
    private FoodCategoriesService foodCategoriesService;

    /**
     * 获取所有食物类型数据
     * @return 食物类型列表
     */
    @GetMapping("/list")
    public ResponseResult<List<FoodCategories>> getAllFoodCategories() {
        try {
            // 查询所有食物分类
            List<FoodCategories> categories = foodCategoriesService.list();
            return ResponseResult.success("获取食物类型成功", categories);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("获取食物类型失败");
        }
    }

    /**
     * 获取食物分类树结构，支持多级分类
     * @return 食物分类树结构
     */
    @GetMapping("/tree")
    public ResponseResult<List<FoodCategories>> getFoodCategoryTree() {
        try {
            // 查询所有食物分类
            List<FoodCategories> categories = foodCategoriesService.list();
            return ResponseResult.success("获取食物类型树结构成功", categories);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("获取食物类型树结构失败");
        }
    }
}